import 'package:flutter/material.dart';
import 'package:flutter_foodybite/Global.dart';
import 'package:flutter_foodybite/provider/LocaleModel.dart';
import 'package:flutter_foodybite/provider/ThemeModel.dart';
import 'package:flutter_foodybite/provider/UserModel.dart';
import 'package:flutter_foodybite/screens/homeroute.dart';
import 'package:flutter_foodybite/screens/main_screen.dart';
import 'package:flutter_foodybite/util/const.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import 'l10n/localization_intl.dart';
import 'screens/loginroute.dart';

void main() async {
  Global.init().then((e) => runApp(MyApp()));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          // ChangeNotifierProvider.value(value: ThemeModeProvider()),
          ChangeNotifierProvider<ThemeModel>(
            create: (context) => ThemeModel(),
          ),
          ChangeNotifierProvider<UserModel>(
            create: (context) => UserModel(),
          ),
          ChangeNotifierProvider<LocaleModel>(
            create: (context) => LocaleModel(),
          )
        ],
        child: Consumer2<ThemeModel, LocaleModel>(
          builder: (context, themeMode, localeModel, _) {
            debugPrint("Consumer changed themeMode=$themeMode localeModel=$localeModel");
            return MaterialApp(
              navigatorKey: Constants.navigatorKey,
              debugShowCheckedModeBanner: false,
              onGenerateTitle: (context) {
                return AppLocalizations.of(context)!.helloWorld;
              },
              locale: localeModel.getLocale(),
              theme: themeMode.darkMode == 1
                  ? Constants.darkTheme
                  : Constants.lightTheme,
              darkTheme: themeMode.darkMode == 2 ? Constants.darkTheme : null,
              home: HomeRoute(),
              localizationsDelegates: [
                // 本地化的代理类
                AppLocalizations.delegate,
                // MyLocalizations.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate
              ],
              supportedLocales: AppLocalizations.supportedLocales,
              localeResolutionCallback:
                  (Locale? _locale, Iterable<Locale> supportedLocales) {
                if (localeModel.getLocale() != null) {
                  //如果已经选定语言，则不跟随系统
                  return localeModel.getLocale();
                } else {
                  Locale? locale;
                  //APP语言跟随系统语言，如果系统语言不是中文简体或美国英语，
                  //则默认使用美国英语
                  if (supportedLocales.contains(_locale)) {
                    locale = _locale;
                  } else {
                    locale = Locale('en', 'US');
                  }
                  return locale;
                }
              },
              // 注册命名路由表
              routes: <String, WidgetBuilder>{
                "login": (context) => LoginRoute(),
                // "themes": (context) => ThemeChangeRoute(),
                // "language": (context) => LanguageRoute(),
              },
            );
          },
        ));
  }

  Widget? createApp(int mode) {}
}
